
#ifdef ELS_OS_MCU
#include "els.h"
#include "els_libload.h"
#include<stdio.h>
#include<string.h>
char els_buff_tmp[1024];
ELS_API void els_lib_init(els_VmObj *l){
    els_register(l,"import",els_lib_load);
    #ifdef ELS_LANG_CN
       els_register(l,"引",els_lib_load); 
    #endif
}
ELS_API int els_lib_load(els_VmObj *l){
    int i;
    const char* s = els_getstr(l,1);
    for(i=0; i<sizeof(els_libs)/sizeof(els_libs[0]); i++){
        if(!strcmp(els_libs[i].name,s)){ 
            els_libs[i].func(l);
            return 0;
        }
    }
    printf("Error: Couldn't Load Library: %s\n",s);
    return 1;
}
#endif

#ifdef ELS_OS_LINUX

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include<unistd.h>
#include "els.h"
#include<dlfcn.h>
char els_buff_tmp[1024];
ELS_API void els_lib_init(els_VmObj *l){
    els_register(l,"import",els_lib_load);
    #ifdef ELS_LANG_CN
       els_register(l,"引",els_lib_load); 
    #endif
}
ELS_API int els_lib_load(els_VmObj *l){
    char tmp[512];
    const char* libfile = els_getstr(l,1);
    typedef void (*api)(els_VmObj* l);
    sprintf(tmp,"./ElsLib_%s.lsd",libfile);
    void* dlibptr = dlopen(tmp,RTLD_LAZY);
   if(dlibptr==NULL){ 
        sprintf(tmp,"/els/lib/ElsLib_%s.lsd",libfile);
        dlibptr = dlopen(tmp,RTLD_LAZY);
    }
    if(dlibptr==NULL) {
        sprintf(tmp,"LibError: Couldn't find Library\t%s\n",libfile);
        els_error(l,tmp);
    }
    sprintf(tmp,"ElsLib_%s_libinit",libfile);
    api func = (api)dlsym(dlibptr,tmp);
    if(func==NULL){ 
        sprintf(tmp,"LibError: Couldn't find Library\t%s\n",libfile);
        els_error(l,tmp);
    }
    func(l);
    return 0;
}

#endif

#ifdef ELS_OS_WINDOWS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<windows.h>

#include "els.h"
#include "els_libload.h"
char els_buff_tmp[1024];

char* els_lib_win_togbk(const char * str);
char* els_lib_win_toutf8(const char* str);

char* els_lib_win_togbk(const char * str){
	int len = MultiByteToWideChar(CP_UTF8,0,str,-1,NULL,0);
	wchar_t *strUnicode = (wchar_t*)malloc(len*sizeof(wchar_t*));
	wmemset(strUnicode,0,len);
	MultiByteToWideChar(CP_UTF8,0,str,-1,strUnicode,len);
	len = WideCharToMultiByte(CP_ACP,0,strUnicode,-1,NULL,0,NULL,NULL);
	char* strGbk = (char*)malloc(len*sizeof(char*));
	memset(strGbk,0,len);
	WideCharToMultiByte(CP_ACP,0,strUnicode,-1,strGbk,len,NULL,NULL);
	free(strUnicode);
	strUnicode=NULL;
	return strGbk;
}
char* els_lib_win_toutf8(const char* str){
	int len = MultiByteToWideChar(CP_ACP,0,str,-1,NULL,0);
	wchar_t* strUnicode = (wchar_t*)malloc(len*sizeof(wchar_t*));
	wmemset(strUnicode,0,len);
	MultiByteToWideChar(CP_ACP,0,str,-1,strUnicode,len);
	len = WideCharToMultiByte(CP_UTF8,0,strUnicode,-1,NULL,0,NULL,NULL);
	char* strutf8 = (char*)malloc(len*sizeof(char*));
    memset(strutf8,0,len);
	WideCharToMultiByte(CP_UTF8,0,strUnicode,-1,strutf8,len,NULL,NULL);
	free(strUnicode);
	strUnicode=NULL;
	return strutf8;
}


ELS_API void els_lib_init(els_VmObj *l){
    els_register(l,"import",els_lib_load);
    #ifdef ELS_LANG_CN
       els_register(l,"引",els_lib_load); 
    #endif
}
ELS_API int els_lib_load(els_VmObj *l){
    int i;
    const char* s = els_getstr(l,1);
    for(i=0; i<sizeof(els_libs)/sizeof(els_libs[0]); i++){
        if(!strcmp(els_libs[i].name,s)){ 
            els_libs[i].func(l);
            return 0;
        }
    }
    printf("Error: Couldn't Load Library: %s\n",s);
    return 1;
}

#endif